package com.yutian.selfservice.support.dict;

import com.yutian.selfservice.support.mapper.DictDataMapper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Configuration
@Slf4j
public class DictConfig {
    private Map<String, Map<String,String>> dict;
    @Autowired
    private DictDataMapper dictDataMapper;
    @PostConstruct
    public void init(){
        List<DictData> list = dictDataMapper.selectDict();
        dict = list.stream().collect(Collectors.groupingBy(DictData::getType,
                Collectors.toMap(DictData::getCode,DictData::getValue)));
        log.info("dict init ");
    }

    public String getValue(String type,String code) {
        try {
            return dict.get(type).get(code);
        } catch (Exception e) {
            log.warn("数据字典获取错误");
            return null;
        }
    }

    @Data
    public static class DictData{
        private String type;
        private String code;
        private String value;
    }
}
